package com.jackeysun.app_util

import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.jackeysun.app_util.databinding.ActivityMainBinding
import com.jackeysun.util.*
import com.jackeysun.util.Application.mApplication
import com.jackeysun.util.SpExt.mStorage

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val mainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mainBinding.root)

        mApplication = this@MainActivity

        mStorage.put("key", "string")
        val s: String = mStorage.getString("key")
        s.toast()

        mainBinding.scale.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
            mainBinding.ivNew.setImageBitmap(bitmap scale 0.5f)
        }

        mainBinding.rotate.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
            mainBinding.ivNew.setImageBitmap(bitmap rotate 90f)
        }

        mainBinding.translateX.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
            mainBinding.ivNew.setImageBitmap(bitmap translateX 20f)
        }

        mainBinding.translateY.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
            mainBinding.ivNew.setImageBitmap(bitmap translateY 20f)
        }

        mainBinding.crop.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
            val rect = Rect()
            mainBinding.ivNew.getLocalVisibleRect(rect)
            rect.set(
                rect.left,
                rect.top,
                (rect.right - rect.left) / 2 + rect.left,
                (rect.bottom - rect.top) / 2 + rect.top
            )
            mainBinding.ivNew.setImageBitmap(
                bitmap crop rect
            )
        }
        mainBinding.composite.setOnClickListener {
            val bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
            mainBinding.ivNew.setImageBitmap(bitmap.combination {
                this.preScale(0.5f, 0.5f)
                this.preRotate(100f)
            })
        }


//        mainBinding.et.postDelayed({ mainBinding.et.bindSoftInput() }, 500)//部分手机延迟才能弹出

//        "keyboard isActive: ${isKeyboardShow()} ".toast()

        mainBinding.toggleSoftInput.setOnClickListener {
            toggleSoftInput()
        }

        mainBinding.bindSoftInput.setOnClickListener {
            mainBinding.et.bindSoftInput()
        }

        mainBinding.unbindSoftInput.setOnClickListener {
            mainBinding.et.unbindSoftInput()
        }
        mainBinding.forceHideSoftInput.setOnClickListener {
            forceHideSoftInput()
        }

        keyboardHeight { height, isShowing ->
            "keyboard height: $height, keyboard isShowing: $isShowing".toast()
//            "keyboard isActive: ${isKeyboardShow()} ".toast()
        }

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_util, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.recyclerview) {
            startActivity(Intent(this@MainActivity, MainActivity2::class.java))
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}